/**
 * 公共方法
 */

/**
 * 设置cookies
 * @param {*} name 
 * @param {*} value 
 * @param {*} perpetual 
 */
// name 字段名   value 字段值   perpetual  有效期
export function setCookie(name, value, perpetual) {
  const exdate = new Date()
  // exdate.setDate(exdate.getDate() + 365)
  exdate.setDate(exdate.getDate() + perpetual)
  document.cookie = name + '=' + value + ';expires=' + exdate.toGMTString()
  // 永久有效
  // document.cookie = name + '=' + value + ';expires=' + 'Fri, 31 Dec 9999 23:59:59 GMT'
}

/**
 * 获取cookies
 * @param {*} name 
 * @returns 
 */
// name 字段名
export function getCookie(name) {
  if (document.cookie.length > 0) {
    let start = document.cookie.indexOf(name + '=')
    if (start !== -1) {
      start = start + name.length + 1
      let end = document.cookie.indexOf(';', start)
      if (end === -1) end = document.cookie.length
      return unescape(document.cookie.substring(start, end))
    }
  }
  return ''
}

/**
 * 删除cookies
 * @param {*} name 
 */
export function deleteCookie(name) {
  document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
}
/**
 * 深拷贝
 * @param {*} obj 
 * @returns 
 */

export function deepClone(obj) {
  if (typeof obj !== 'object' || obj === null) {
    return obj
  }
  let copy = Array.isArray(obj) ? [] : {}
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      copy[key] = deepClone(obj[key])
    }
  }
  return copy
}
